<template>
<div class='article-container'>
    <!-- 导航栏 -->
     <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      :title="article.title"
    ></van-nav-bar>
    <!-- 加载中 -->
    <van-loading v-if="loading" class="article-loading" />
    <!-- 文章详情 -->
     <div class="detail" v-else>
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image round width="1rem"
         height="1rem"
         fit="fill"
         :src="article.aut_photo"
          />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | relativeTime}}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="hSwitchFollow"
        >{{article.is_followed ? '取关' : '+ 关注'}}</van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button round size="small" hairline type="primary" plain icon="good-job-o">点赞</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button round size="small" hairline type="danger" plain icon="delete">不喜欢</van-button>
      </div>
    </div>
    <!-- 文章评论 -->
    <article-comment :article_id="$route.params.id"></article-comment>
</div>
</template>
<script>
// 获取文章详情接口
import { getArticle } from '@/api/articles'
import { followUser, unFollowUser } from '@/api/user'
import ArticleComment from './comment'
export default {
  name: 'ArticleIndex',
  components: {
    ArticleComment
  },
  data () {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: { } // 当前文章
    }
  },
  created () {
    //   页面一加载  就发送请求获取数据 渲染页面
    this.loadArticle()
  },
  beforeRouteLeave (to, from, next) {
    console.log('你现在在文章详情页，你将要离开这个页面')
    console.log(to, from)
    if (to.path === '/login' && this.$store.state.tokenInfo.token) {
      next({ path: '/' })
    } else {
      next()
    }
  },
  methods: {
    //   关注和取消关注 如果这篇文章是自已写的。则不能自已关注自已！
    async hSwitchFollow () {
      try {
        const isFollowed = this.article.is_followed
        const authID = this.article.aut_id
        // 根据当前的is_followed的值决定调用哪个 方法
        // 如果 is_followed是true,在点击按钮，说明要做取关
        if (isFollowed) {
          const res = await unFollowUser(authID)
          console.log(res)
        } else {
          const res = await followUser(authID)
          console.log(res)
        }
        // 更新视图
        // 在本地数据中进行取反
        this.article.is_followed = !isFollowed
        this.$toast.success('操作成功')
      } catch (e) {
        this.$toast.fail('操作成功')
      }
    },
    //  发送请求  获取数据
    async loadArticle () {
      const id = this.$route.params.id
      //   发送请求
      const res = await getArticle(id)
      console.log(res)
      //   数据赋值
      this.article = res.data.data
      //   关闭loading状态
      this.loading = false
      console.log(this.article)
    }
  }
}
</script>
 <style lang='less' scoped>
 .article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
